/**
 * 诊疗阶段关联业务初始化
 */
var StageRelation = {
    ztree: null,
    parentId: 0,
    entityId: -1,
    compare: 'match'
};

/**
 * 删除诊疗方案关联
 */
StageRelation.del = function (id, name) {
    var ajax = new $ax(Sys.backendPath + "/sdc/sdcstagerelation/delete", function (data) {
        if (data.code === 200) {
            $.messager.popover({msg: '取消关联成功！', type:'success', timeout: 1000});
            StageRelation.search(StageRelation.compare);
            flag = true;
        } else {
            Sys.error(data.message);
        }
    }, function (data) {
        response = JSON.parse(data.responseText);
        Sys.error(response.message);
    });
    ajax.set("stageRelationId", id);
    ajax.start();
};

StageRelation.onClickRel = function (e, treeId, treeNode) {
    if (treeNode.pId == null || treeNode.pId === 0) {
        Sys.info("不能选择根节点（"+treeNode.name+"），请重新选择具体阶段。");
        return false;
    }
    $('#NullTab').hide();
    $('#custtb').show();
    $("#searchKey").searchbox("setValue", "");
    StageRelation.parentId = treeNode.id;
    StageRelation.entityId = treeNode.pId.substr(1);
    StageRelation.search('match');
};

/**
 * 添加诊疗方案
 */
StageRelation.add = function (stageId, treatPlanId, name) {
    //提交信息
    var paramData = {};
    var ajax = new $ax(Sys.backendPath + "/sdc/sdcstagerelation/add", function (data) {
        $.messager.popover({msg: '关联成功！',type:'success',timeout: 1000});
        StageRelation.search(StageRelation.compare);
    }, function (data) {
        response = JSON.parse(data.responseText);
        Sys.error(response.message);
    });
    paramData['stageId'] = stageId;
    paramData['treatPlanId'] = treatPlanId;
    ajax.setData(paramData);
    ajax.start();
}

StageRelation.search = function (compare) {
    StageRelation.compare = compare;
    StageRelation.stageRelationMatch = $HUI.datagrid("#stageRelationMatch", {
        fit: true,
        pagination: true, //如果为true, 则在DataGrid控件底部显示分页工具栏
        displayMsg: "当前表格显示 {from} 到 {to} ,共 {total} 条记录",
        singleSelect: false,
        //定义是否设置基于该行内容的行高度。设置为 false，则可以提高加载性能
        autoRowHeight: false,
        striped: true,
        rownumbers: true,
        checkOnSelect: false,
        selectOnCheck: true,
        loadMsg: '数据加载中...',
        url: "/sdc/sdcstagerelation/getTreatPlanList",
        method: 'post',
        queryParams: {
            stageId: StageRelation.parentId,
            treatName: $("#searchKey").searchbox('getValue'),
            compare: compare,
            qcentityId: StageRelation.entityId
        },
        pageSize: 10,
        pageList: [10,50],
        idField: "id",
        columns: [[
            {field:'checked', checkbox:'true', align:'center', width:30, auto:false},
            {field:'itemName', title:'诊疗方案名称', width:350},
            {field:'itemTypeName', title:'诊疗方案类型', width:150},
            {field:'itemDesc', title:'方案备注', width:450},
        ]],
        onCheck: function (rindex, rowData) {
            if (rowData.checked !==1 ) {
                StageRelation.add(StageRelation.parentId, rowData.itemId, rowData.itemName);
            }
        },
        onUncheck: function(rindex, rowData) {
            if (rowData.checked === 1) {
                StageRelation.del(rowData.id, rowData.itemName);
            }
        },
        onLoadSuccess: function (rowData) {
            $('#stageRelationMatch').datagrid('clearChecked');
            $(".datagrid-header-check").html("");
            var rows = rowData.rows;
            $.each(rows, function (idx, val) {
                if (val.checked === 1){
                    $('#stageRelationMatch').datagrid('checkRow', idx);
                } else {
                    $('#stageRelationMatch').datagrid('uncheckRow', idx);
                }
            })
        }
    });
}

$(function () {
    var ztree = new $ZTree("stageTree", Sys.backendPath + "/sdc/sdcstagerelation/tree");
    ztree.bindOnClick(StageRelation.onClickRel);
    ztree.init();
    StageRelation.ztree = ztree;
    fuzzySearch('stageTree','#seachKey',null,true);
    $('#searchKey').searchbox({
        searcher: function(value){
            StageRelation.search(StageRelation.compare);
        },
        prompt:'方案关键字'
    });
    $('#custtb').hide();
});
